<template>
  <div>
    <el-dialog
      title="添加海报"
      :visible.sync="addPosterDialog"
      width="30%"
      @close="close"
    >
      <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
        <el-form-item label="海报名称" prop="name">
          <el-input style="" v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="背景图" prop="file">
          <el-upload
            v-model="formData.file"
            action="#"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :auto-upload="false"
            :file-list="fileList"
            :limit="1"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <!-- <el-form-item label="排序" prop="sortId">
          <el-input v-model.number="formData.sortId"></el-input>
        </el-form-item> -->
        <el-form-item label="是否显示" prop="isDisplay">
          <el-switch v-model.number="formData.isDisplay"></el-switch>
        </el-form-item>
        <el-form-item label="">
          <el-row :gutter="10">
            <el-col :span="6">
              <el-button type="primary" @click="btnOk">保存</el-button>
            </el-col>
            <el-col :span="6">
              <el-button @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { addPosterApi } from "@/api/application";
export default {
  props: {
    addPosterDialog: {
      type: Boolean,
    },
  },
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      formData: {
        file: "",
        sortId: 0,
        name: "",
        type: 1,
        isDisplay: true,
      },
      fileList: [],
      rules: {
        file: [
          { required: true, validator: this.validateImage, trigger: "change" },
        ],
        sortId: [{ required: true, message: "请输入排序", trigger: "blur" }],
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        type: [{ required: true, message: "请输入排序", trigger: "blur" }],
        isDisplay: [
          { required: true, message: "请选择是否显示", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    // 图片校验
    validateImage(rule, value, callback) {
      // 自定义图片校验逻辑
      const isValid = this.fileList.length > 0; // 检验是否有文件被选中
      if (!isValid) {
        callback(new Error("请选择图片"));
      } else {
        callback();
      }
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      this.formData.file = ""; // 清空文件数据
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 选择图片
    handleChange(file, fileList) {
      // 限制只保留最新上传的文件
      if (fileList.length > 1) {
        fileList = fileList.slice(-1);
      }
      this.fileList = fileList;
      this.formData.file = file.raw;
    },
    close() {
      this.$emit("update:addPosterDialog", false);
      // 重置表单数据
      this.formData = {
        file: "",
        sortId: 0,
        name: "",
        type: 1,
        isDisplay: true,
      };
      // 清空文件列表
      this.fileList = [];
      // 重置表单校验结果
      this.$refs.form && this.$refs.form.resetFields();
      // 清空预览图
      this.dialogImageUrl = "";
    },
    // 保存
    async btnOk() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            const formData = new FormData();
            formData.append("file", this.formData.file);
            formData.append("name", this.formData.name);
            formData.append("type", this.formData.type);
            formData.append("sortId", this.formData.sortId);
            formData.append(
              "isDisplay",
              this.formData.isDisplay === true ? 1 : 0
            );

            const res = await addPosterApi(formData);
            if (res.code === 1) {
              this.$message.success("添加成功!");
              this.$emit("update:addPosterDialog", false);
              this.$parent.selPosterList();
              // 重置表单数据
              this.close();
            } else {
              this.$message.error("添加失败");
            }
          } catch (error) {
            console.error("添加失败:", error);
            this.$message.error("添加失败");
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
